<?php

namespace app\business\controller;

use think\Controller;
use think\facade\Env;
use app\models\{Shoplist, Goods, Cate};
use app\facade\Redis;
use think\Db;
use oss\Oss;

class Base extends Controller
{

    public function initialize()
    {
        $menu = config('bmenu.menu');
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        $path = [];
        if (isset($menu[$controller])) {
            $path['c'] = $menu[$controller]['name'] ?: null;
            $path['a'] = $menu[$controller]['sub'][$action]['name'] ?: null;
            $path['s'] = $menu[$controller]['sub'][$action]['show'] ?: null;
        }
        $this->assign('path', $path);
    }

    public function message()
    {
        $name = 'ordermessage_' . $this->request->bussiness['id'];
        $length = Redis::lLen($name);
        if ($length > 0) {
            Redis::del($name);
            return json(['code' => 0, 'msg' => '您有' . $length . '个待处理订单!!!']);
        }
        return json(['code' => 1, 'msg' => '暂无订单']);
    }

    protected function get_rand_str($len = 6)
    {
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'), array('$', '@', '#', '%', '&'));
        shuffle($arr);
        $sub_arr = array_slice($arr, 0, $len);
        return implode('', $sub_arr);
    }

    public function upload_img()
    {
        if ($this->request->isPost()) {
            $file = request()->file('file');
            $path =  $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
            $info = $file->validate(['size' => 1024 * 1024 * 5, 'ext' => 'jpg,png,gif,jpeg'])
                ->move($path);
            if (!$info) {
                return json(['code' => 1, 'data' => []]);
                $this->result([], 1, $file->getError());
            } else {
                $url = '/uploads' . '/' . date("Ymd") . '/' . $info->getfileName();
                $url = url($url, '', '', true);
                return json(['code' => 0, 'data' => ['src' => $url]]);
            }
        }
    }

    public function gettypes()
    {
        $catemodel = new Cate();
        $goodsmodel = new Goods();
        $type = $this->request->get('type/d', 0);
        $shopid = $this->request->get('shopid/d', 0);
        switch ($type) {
            case '1':
                $result = $goodsmodel->where(['shopid' => $shopid, 'isSale' => 1, 'isdel' => 1])->field('id,title')->select();
                break;
            case '2':
                $result = $catemodel->where(['shopid' => $shopid])->field('id,name as title')->select();
                break;
            default:
                return json(['code' => 1, 'data' => []]);
                break;
        }
        if (count($result) > 0) {
            return json(['code' => 0, 'data' => $result]);
        }
        return json(['code' => 1, 'data' => []]);
    }

    public function shoplist()
    {
        $model = new Shoplist();
        $title = $this->request->get('title', '');
        $result = $model->field('id,title,account')->where('title|account', 'like', '%' . $title . '%')->limit(10)->select();
        if (count($result) > 0) {
            return json(['code' => 0, 'data' => $result]);
        }
        return json(['code' => 1, 'data' => []]);
    }

    public function getaddress()
    {
        $keyword = $this->request->get('keyword/s', ''); //关键词
        $url = 'https://apis.map.qq.com/ws/place/v1/suggestion';
        $result = api_curl($url, ['keyword' => $keyword, 'key' => 'UHRBZ-EYNKG-MKSQO-IEY66-FHOGV-W3FKM']);
        $result = json_decode($result, true);
        if (!empty($result) && $result['status'] == 0 && count($result['data']) > 0) {
            return json(['code' => 0, 'data' => $result['data']]);
        }
        return json(['code' => 1, 'data' => []]);
    }
}
